package day05;

import day02.Order.User;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/24
 */
//深拷贝
public class ObjectDemo2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        double[] scores={90.0,87.5,78,100,99.0};
        Person1 p1 = new Person1("小明",16,scores);
        Person1 p2 = (Person1) p1.clone();
        double[] newscores=p1.getScores();
        newscores[0]=100;
        System.out.println(p1);
        System.out.println(p2);
    }
}
class Person1 implements Cloneable{
    private String name;
    private int age;
    double[] scores;
    public Person1() {
    }

    public Person1(String name, int age, double[] scores) {
        this.name = name;
        this.age = age;
        this.scores = scores;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return scores
     */
    public double[] getScores() {
        return scores;
    }

    /**
     * 设置
     * @param scores
     */
    public void setScores(double[] scores) {
        this.scores = scores;
    }

    public String scoreTostring(){
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < scores.length; i++) {
            sb.append(scores[i]);
            if(i!=scores.length-1){
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }
    public String toString() {
        return "Person{name = " + name + ", age = " + age + ", scores = " + scoreTostring()+ "}";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        double[] sco=this.scores;
        double[] newscores=new double[sco.length];
        for(int i=0;i<sco.length;i++){
            newscores[i]=sco[i];
        }
        Person1 p_1=(Person1)super.clone();
        p_1.scores=newscores;
        return p_1;
    }
}
